package com.javelingroup.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javelingroup.data.Data;
import com.javelingroup.data.LoadFile;

@WebServlet("/control")
public class Control extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String breadCrumb = request.getParameter("breadCrumb");
		String navigateTo = "/navigation";
		
        if (breadCrumb == null) {
            breadCrumb = "";
        }
        
        if (breadCrumb.contains(".avi")){
            navigateTo="/playit" + "?breadCrumb=" + breadCrumb;
        }else{
        	navigateTo="/navigation" + "?breadCrumb=" + breadCrumb;
        }
        
        
        RequestDispatcher rd = request.getRequestDispatcher(navigateTo);
        rd.forward(request, response);
		
	}
	
    public void init() throws ServletException {
        super.init();
        
        //load the media data information
        LoadFile lf = new LoadFile();
        List<Data> data = lf.ReadFile();
        
        //put all media data information into global use
        ServletContext sc = getServletContext();
        sc.setAttribute("Data", data);
    }

}
